package remove_middle;

import java.io.BufferedWriter;
import java.io.FileWriter;

import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

public class Driver {

	/**
	 * @param args
	 */
	static String dataSet = "TRAIN";
	static String before = "";
	static String path = "C:/Users/Dan/Documents/My Dropbox/2011 Fall Class (Emotional Speech)/ALC/";
	
	public static void main(String[] args) throws Exception {
		System.out.println("Reading in data...");
		long start = System.currentTimeMillis();
        DataSource initialData = new DataSource(path + "IS11." + dataSet + "_fbg.arff");
        Instances initialDataInstances = initialData.getDataSet();
        System.out.println("data read in " + (System.currentTimeMillis()-start)/1000 + " seconds");
        
        DataSource initialBac = new DataSource(path + "BAC." + dataSet + "_b.arff");
        Instances initialBacInstances = initialBac.getDataSet();
        int outsideRange = 0, insideRange = 0;
        int outOldRange = 0, inOldRange = 0;
        final int BAC_INDEX = 1;
        final double LOWER_THRESHOLD = 0.0002;
        final double UPPER_THRESHOLD = 0.0008;
        for(int i=0; i < initialBacInstances.numInstances(); i++) {
        	double value = initialBacInstances.instance(i).value(BAC_INDEX);
        	if( value > 0.0008 && value <= 0.0016 )
        		inOldRange++;
        	else
        		outOldRange++;
        	if(value > LOWER_THRESHOLD && value <= UPPER_THRESHOLD) {
        		insideRange++;
        		initialDataInstances.delete(i);
        		initialBacInstances.delete(i);
        		i--;
        	}
        	else {
        		outsideRange++;
        	}
        }
        System.out.println("Within range: "+insideRange+", Outside range: "+outsideRange);
        
        System.out.println("Writing data set without middle " + insideRange / (double)(insideRange + outsideRange) * 100 + "%");
        System.out.println("Old range removed: " + inOldRange / (double)(inOldRange + outOldRange) * 100 + "%");
        BufferedWriter writer = new BufferedWriter( new FileWriter(path + "IS11."+dataSet+"_fbgm.arff"));
        writer.write(initialDataInstances.toString());
        writer.newLine();
        writer.flush();
        writer.close();
        
        writer = new BufferedWriter( new FileWriter(path + "BAC."+dataSet+"_bm.arff"));
        writer.write(initialDataInstances.toString());
        writer.newLine();
        writer.flush();
        writer.close();
        System.out.println("Done writing");
	}

}
